package com.spark.rectify

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.ImageView
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.common.BaseActivity
import kotlinx.android.synthetic.main.frag_selector_chk_std.*

/**
 * 标准不安全行为列表
 */
class StdUnsafeActionListActivity : BaseActivity() {
    private var dirSelect: DirFilterDF? = null
    private var keySelect: KeywordFilterDF? = null
    private var selectPos = -1
    private var dir: String = ""
    private var keyword: String = ""
    private lateinit var stdUnsafeActionListAdapter: StdUnsafeActioinListAdapter

    companion object {
        const val resStdUnsafeActionList = 0x4646

        fun actionStartBack(host: Activity, reqCode: Int) {
            val intent = Intent(host, StdUnsafeActionListActivity::class.java)

            host.startActivityForResult(intent, reqCode)
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_std_unsafe_action_list
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        iv_back.setOnClickListener { pop() }
        tv_title.text = "选择不安全行为"
        // dir
        tv_directory.setOnClickListener {
            selectPos = 0
            dirSelect = DirFilterDF.newInstance(object : OnFilterSelectListener {
                override fun onFilterChecked(keyword: String) {
                    dir = keyword
                    LogUtils.e("【dir】$dir")
                }
            })
            dirSelect?.showNow(supportFragmentManager, "directory")
            keySelect?.dismiss()
            updateSelectState(selectPos)
        }
        // keyword
        tv_keyword.setOnClickListener {
            selectPos = 1
            keySelect = KeywordFilterDF.newInstance(object : OnFilterSelectListener {
                override fun onFilterChecked(keyword: String) {
                    this@StdUnsafeActionListActivity.keyword = keyword
                    LogUtils.e("【keyword】$keyword")
                }
            })
            keySelect?.showNow(supportFragmentManager, "keyword")
            dirSelect?.dismiss()
            updateSelectState(selectPos)
        }
        refresh_layout.apply {
            setOnRefreshListener { }
            setOnLoadmoreListener { }
        }
        recy_stds.apply {
            stdUnsafeActionListAdapter = StdUnsafeActioinListAdapter(R.layout.item_selector).apply {
                addCountChangeListener(object : StdUnsafeActioinListAdapter.OnCountChangeListener {
                    override fun onCountChanged(datas: MutableList<StdUnsafeActionListModel>) {
                        LogUtils.e(datas)
                        tv_selected_count.text = "${datas.size}"
                    }

                })
            }
            adapter = stdUnsafeActionListAdapter
            stdUnsafeActionListAdapter.addData(mutableListOf(
                    StdUnsafeActionListModel(keyName = "轻微", descript = "这是轻微的违规"),
                    StdUnsafeActionListModel(keyName = "一般", descript = "这是一般的违规"),
                    StdUnsafeActionListModel(keyName = "重大", descript = "这是重大的违规")
            ))
            layoutManager = LinearLayoutManager(context)
        }
        tv_sure.setOnClickListener {
            val resultIntent = Intent()
            resultIntent.putExtra("actionList", Gson().toJson(stdUnsafeActionListAdapter.summary().toString()))
            setResult(resStdUnsafeActionList, resultIntent)
            finish()
        }
        updateSelectState(selectPos)
    }

    private fun updateSelectState(selectPos: Int) {
        tv_directory.apply {
            if (selectPos == 0) {
                setTextColor(resources.getColor(R.color.blue0092))
                setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_up_blue, 0)
            } else {
                setTextColor(resources.getColor(R.color.black))
                setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_down_blue, 0)
            }
        }
        tv_keyword.apply {
            if (selectPos == 1) {
                setTextColor(resources.getColor(R.color.blue0092))
                setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_up_blue, 0)
            } else {
                setTextColor(resources.getColor(R.color.black))
                setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_down_blue, 0)
            }
        }
    }
}

data class StdUnsafeActionListModel(
        var id: String = "",
        var keyName: String = "",
        var descript: String = "",
        var isChecked: Boolean = false
)

class StdUnsafeActioinListAdapter(layoutRes: Int) : BaseQuickAdapter<StdUnsafeActionListModel, BaseViewHolder>(layoutRes) {
    private var onCountChangeListener: OnCountChangeListener? = null

    override fun convert(helper: BaseViewHolder?, item: StdUnsafeActionListModel?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_level, item.keyName)
            setText(R.id.tv_standard, item.descript)
            getView<ImageView>(R.id.iv_plus).apply {
                item.isChecked = !item.isChecked
                onCountChangeListener?.onCountChanged(summary())
            }
        }
    }

    fun summary(): MutableList<StdUnsafeActionListModel> {
        return data.filter {
            return@filter it.isChecked
        }.toMutableList()
    }

    fun addCountChangeListener(listener: OnCountChangeListener) {
        onCountChangeListener = listener
    }

    interface OnCountChangeListener {
        fun onCountChanged(datas: MutableList<StdUnsafeActionListModel>)
    }
}